//
//  WiiSegue.m
//  iFeller
//
//  Created by Wii on 15/12/11.
//  Copyright © 2015年 Wii. All rights reserved.
//

#import "WiiSegue.h"

@implementation WiiSegue

- (void)perform{
    UIViewController *vc = self.destinationViewController;
    UIView *materialView = ({
        CGRect viewFrame = self.targetButton.frame;
        CGFloat width = MAX(CGRectGetWidth(self.targetButton.frame), CGRectGetHeight(self.targetButton.frame));
        viewFrame.size = CGSizeMake(width, width);
        UIView *view = [[UIView alloc] initWithFrame:viewFrame];
        view.backgroundColor = self.animationColor;
        view.layer.cornerRadius = width / 2.0;
        view;
    });
    
    [[UIApplication sharedApplication].keyWindow addSubview:materialView]; //显示
    CGFloat size = MAX(CGRectGetHeight(vc.view.frame),
                       CGRectGetWidth(vc.view.frame)) * 2.0;
    CGFloat scale = size / CGRectGetWidth(materialView.frame);
    CGAffineTransform finalTransform = CGAffineTransformMakeScale(scale, scale);
    
    [UIView animateWithDuration:0.5 animations:^{
        materialView.transform = finalTransform;
        materialView.center = vc.view.center;
        materialView.backgroundColor = vc.view.backgroundColor;
    } completion:^(BOOL finished) {
        //显示ViewController
        [self.sourceViewController presentViewController:vc animated:NO completion:nil];
        [UIView animateWithDuration:0.25 animations:^{
            materialView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [materialView removeFromSuperview];
        }];
    }];
    
}

@end
